<?php

class DetailAction extends Action {
    public function index(){
    		if(empty($_GET['id'])){
    			$_GET['id'] = 1;
    		}
    		session('goodid',$_GET['id']);
    		$this->display();
    } 
    public function getDetail(){
    		$info['id']=$_SESSION['goodid'];
    		$goods = M("good");
    		$list=$goods->where($info)->select();
    		
    		foreach($list as $k=>$v){
    			$list[$k]["imgurl"]=explode(',',$v["imgurl"]);
    			$list[$k]["type"]=explode(',',$v["type"]);
    			$list[$k]["detailimg"]=explode(',',$v["detailimg"]);
    		}
    		$res['data']=$list;
    		$this->ajaxReturn($res);
    }
    public function addshopcar(){
	    	if(!empty($_SESSION['username'])){
	    		$person['username'] = $_SESSION['username'];
	    		$users = M("users");
	    		$data = $users->where($person)->find();
	    		
	    		$shopcar['goodid'] = $_POST['goodid'];
	    		$shopcar1['count'] = $_POST['count'];
	    		$shopcar['type'] = $_POST['type'];
	    		$shopcar['userid'] = $data['id'];
	    		
	    		$shopcart = M("shopcar");
	    		$res = $shopcart->where($shopcar)->find();
	    		if($res){
	    			$shopcar['count']=$res['count']*1+$_POST['count'];
	    			$result = $shopcart->where('id='.$res['id'])->save($shopcar);
	    			if($result){
	    				$info['data']="添加成功";
	    				$info['status']=1;
	    			}else{
	    				$info['data']="添加失败";
	    				$info['status']=2;
	    				$info['res1']=$result;
	    			}
	    		}else{
	    			$shopcar['count']=$_POST['count'];
	    			$result = $shopcart->add($shopcar);
	    			if($result){
	    				$info['data']="添加成功";
	    				$info['status']=1;
	    			}else{
	    				$info['data']="添加失败";
	    				$info['status']=2;
	    			}
	    		}
	    	}else{
	    		$info['data']="没登陆";
	    		$info['status']=3;
	    	}
    		$this->ajaxReturn($info);
    }
    public function getshopcar(){
    		$person['username'] = $_SESSION['username'];
    		$users = M("users");
    		$data = $users->where($person)->find();
    		$shopcar['userid'] = $data['id'];
    		$shopcart = M("shopcar");
    		$res = $shopcart->where($shopcar)->select();
    		if($res){
    			for($i = 0;$i<count($res);$i++){
	    			$goods = M("good");
	    			$good['id']=$res[$i]['goodid'];
	    			$goodinfo=$goods->where($good)->find();
		    		$goodinfo["imgurl"]=explode(',',$goodinfo["imgurl"])[0];
	    			$info['goodinfo'][$i]=$goodinfo;
	    		}
    			$info['data']=$res;
    			$info['count']=$res[0]['goodid'];
    			$info['status']=1;
    		}else{
    			$info['data']="";
    			$info['status']=2;
    		}
    		$this->ajaxReturn($info);
    		
    }
    
    
    public function exitlogin(){
    		session("username",null);
    		$info['info']="清除成功";
    		$info['status']=1;
    		$this->ajaxReturn($info);
    }
    
    public function getuserlove(){
		$numbers = range (5,354);
		shuffle ($numbers);
		$result = array_slice($numbers,0,5);
		$goods = M("good");
		for($i=0;$i<count($result);$i++){
			$info['id']=$result[$i];
	    		$list=$goods->where($info)->select();
	    		$list[0]['imgurl'] = explode(",",$list[0]['imgurl'])[0];
	    		$res[$i]=$list;
		}
		$this->ajaxReturn($res);
    }
    
    public function gethotgood(){
		$numbers = range (5,354);
		shuffle ($numbers);
		$result = array_slice($numbers,0,5);
		$goods = M("good");
		for($i=0;$i<count($result);$i++){
			$info['id']=$result[$i];
	    		$list=$goods->where($info)->select();
	    		$list[0]['imgurl'] = explode(",",$list[0]['imgurl'])[0];
	    		$res[$i]=$list;
		}
		$this->ajaxReturn($res);
    }
    
    
}
?>


